#!/usr/bin/env python

import logging
import wsgiref.handlers
import rest

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from app.controllers import deposits, households, payments, people, sessions
from app.models import deposit, household, payment, person

class AppController(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()
    if not user:
      loginurl = users.create_login_url(self.request.path)
    else:
      logouturl = users.create_logout_url(self.request.path)
    self.redirect("/public/index.html")

def main():
  application = webapp.WSGIApplication(
    [('/*$', AppController), 
     ('/deposits.*', deposits.Controller),
     ('/households.*', households.Controller),
     ('/payments.*', payments.Controller),
     ('/people.*', people.Controller),
     ('/sessions.*', sessions.Controller),
     ('/rest/.*', rest.Dispatcher)
    ], debug=True)
  wsgiref.handlers.CGIHandler().run(application)

# configure the rest dispatcher to know what prefix to expect on request urls
rest.Dispatcher.base_url = "/rest"

# add all models from some other module, and/or...
rest.Dispatcher.add_models({
  "household": household.Household,
  "person": person.Person,
  "deposit": deposit.Deposit,
  "payment": payment.Payment})




if __name__ == '__main__':
  main()
